package com.situ.hotel.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.hotel.entity.User;
import com.situ.hotel.mapper.UserMapper;
import com.situ.hotel.service.UserService;
import com.situ.hotel.util.MD5Util;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public int add(User user) throws Exception {
		if (user.getName() == null || user.getName().length() < 2 || user.getName().length() > 32) {
			throw new Exception("姓名必须是2-32位字符串");
		}
		if (user.getIdcard() == null || user.getIdcard().length() != 18) {
			throw new Exception("身份证号必须是18位");
		}
		if (user.getPhone() == null ||

				user.getPhone().length() != 11) {

			throw new Exception("电话号码必需是11位");
		}
		if (user.getPassword() != null &&

				(user.getPassword().length() < 6 || user.getPassword().length() > 16)) {
			throw new Exception("密码必需为6-16位字符串");

		}

		if (userMapper.selectByIdcard(user.getIdcard()) != null) {
			throw new Exception("身份证号已经存在");
		}
		if (userMapper.selectByPhone(user.getPhone()) != null) {
			throw new Exception("电话号码已经存在");
		}
		if (user.getPassword() == null) {
			user.setPassword(user.getIdcard().substring(user.getIdcard().length() - 6));

		}
		user.setPassword(MD5Util.getMD5(user.getPassword()));
		return userMapper.insert(user);
	}

	@Override
	public int edit(User user) throws Exception {
		// TODO Auto-generated method stub
		if (user.getName() != null && (user.getName().length() < 2
				|| user.getName().length() > 32
		)) {
			throw new Exception("姓名必须是2-32位字符串");
		}
		if (user.getIdcard() != null &&
				user.getIdcard().length() != 18) {
			throw new Exception("身份证号必须是18位");
		}
		if (user.getPhone() != null && user.getPhone().length() != 11

		) {
			throw new Exception("电话号码必需是11位");
		}
		if (user.getPassword() != null &&

				(user.getPassword().length() < 6 || user.getPassword().length() > 16)) {
			throw new Exception("密码必需为6-16位字符串");
		}

		if (userMapper.selectById(user.getId()) == null) {
			throw new Exception("id 不存在");
		}

		User sUser = userMapper.selectByIdcard(user.getIdcard());
		if (sUser != null && sUser.getId() != user.getId()) {
			throw new Exception("身份证号已经存在");
		}

		sUser = userMapper.selectByPhone(user.getPhone());
		if (sUser != null && sUser.getId() != user.getId()) {
			throw new Exception("电话号码已经存在");
		}
		
		if(user.getPassword()!=null)
		user.setPassword(MD5Util.getMD5(user.getPassword()));
		return userMapper.update(user);
	}

	@Override
	public  List<User> search(User user) {
		// TODO Auto-generated method stub
		return userMapper.select(user);
	}

	@Override
	public PageInfo searchByPage(Integer page, Integer limit, User user) {
		if(limit==null) limit=10;
		// TODO Auto-generated method stub
		PageHelper.startPage(page,limit);
		List<User>list=userMapper.select(user);
		PageInfo pageInfo=new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public User getById(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.selectById(userId);
	}

	@Override
	public User login(User user) throws Exception {
		// TODO Auto-generated method stub
		if(user.getPhone()==null || user.getPhone().length()!=11) {
			throw new Exception("电话号码11位啊");
		}
		if(user.getPassword()==null || user.getPassword().length()<3||user.getPassword().length()>16) {
			throw new Exception("密码必需是3-16位的字符");
		}
		User sUser = userMapper.selectByPhone(user.getPhone());
		if(sUser == null) {
			throw new Exception("电话号码未注册");
		}
		
		if(!sUser.getPassword().equals(MD5Util.getMD5(user.getPassword()))) {
			throw new Exception("密码错误");
		}
		
		if(sUser.getStatus()==1) {
			throw new Exception("当前用户已被禁用，请联系管理员");
		}
	//	User sUser = userMapper.selectByPhone(user.getPhone());
		return sUser;

	}

}
